package com.feib.stms.batch.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


import org.springframework.stereotype.Repository;


@Repository("deliveryPaymentChkSumJdbcDao")
public class DeliveryPaymentChkSumJdbcDao extends BaseJdbcDao {



	/**
	 * 取得待驗資資料
	 * @return
	 */
	public List<Map<String, Object>> queryToDoChkUploadFile(Date nextBussinessDay, boolean firstTime)
	{
		StringBuffer sql = new StringBuffer();
		List<Object> args = new ArrayList<Object>();

		sql.append("SELECT UPLOAD_FILE_PK ");
		sql.append(", FILE_FORMAT ");
		sql.append(", FILE_STATUS ");
		sql.append(", DELIVERY_DATE");
		sql.append(" from UPLOAD_FILE ");
		sql.append(" where DELIVERY_DATE >= ? ");

		args.add(nextBussinessDay);

		sql.append(" AND FILE_FORMAT ='2'");

		if (firstTime)
		{
			sql.append(" AND FILE_STATUS = 0");

		}
		else
		{
			sql.append(" AND FILE_STATUS < 5");

		}

		logger.debug("SQL: {}", sql.toString());
		logger.debug("Args: {}", args.toString());


		return this.getJdbcTemplate().queryForList(sql.toString(),args.toArray()); 
	}

	
	public Date getNextBusinessDay(Date day){
		StringBuffer sql = new StringBuffer();
		List<Object> args = new ArrayList<Object>();
		
		sql.append("SELECT min(THE_DATE) as THE_DATE ");
		sql.append(" FROM BUSINESS_DAY WHERE THE_DATE > ? AND STATUS = 'W'");
	
		args.add(day);
		
		logger.debug("SQL: {}", sql.toString());
		logger.debug("Args: {}", args.toString());
		
		List<Map<String, Object>> rds = this.getJdbcTemplate().queryForList(sql.toString(),args.toArray());
        List<Date> result = new ArrayList<Date>();
        for(Map<String, Object> rd : rds)
            result.add((Date)rd.get("THE_DATE"));
        
        rds.clear();
        
        return result.get(0);
		
		
	}
	

}
